from pydantic import BaseModel
from typing import Optional, TypeVar, Generic, Any

T = TypeVar('T')


class ResponseModel(BaseModel, Generic[T]):
    code: int
    msg: str
    data: Optional[T] = None


class ListResponseModel(BaseModel, Generic[T]):
    code: int
    msg: str
    data: list[T] = []
    total: int = 0


def success(data: Any = None, msg: str = "Success") -> dict:
    return {
        "code": 200,
        "msg": msg,
        "data": data
    }


def success_list(data: list = None, total: int = 0, msg: str = "Success") -> dict:
    return {
        "code": 200,
        "msg": msg,
        "data": data or [],
        "total": total
    }


def error(msg: str = "Error", code: int = 400) -> dict:
    return {
        "code": code,
        "msg": msg,
        "data": None
    }